//
//  MySerachViewControllerXinDeman123.m
//  FD
//
//  Created by xiaofang wang on 2019/1/23.
//  Copyright © 2019年 ZhiYuan Network. All rights reserved.
//  输入框样式参考首页查看配送地址页面。
#import "MySerachViewControllerXinDeman123.h"
//view
#import "HomeSearchTextView.h" //自定义输入框view

//google地图
#import <GooglePlaces/GooglePlaces.h>



@interface MySerachViewControllerXinDeman123 ()<GMSAutocompleteTableDataSourceDelegate,
HomeSearchTextViewDelegate>

/**谷歌地图自送搜索数据源类*/
@property (nonatomic, strong) GMSAutocompleteTableDataSource *tableDataSource;
/**搜索结果控制器*/
@property (nonatomic, strong) UITableViewController *resultControler;
/**假的表头*/
@property (nonatomic, strong) UIView *fakeNavView;

/**搜索框*/
@property (nonatomic, strong) HomeSearchTextView *searchView;
/**tableView*/
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MySerachViewControllerXinDeman123

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kMainLightGrayColor;
    [self setupUI];
}

//不要导航条了就像噩梦,幽灵一样,折磨.
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
}

#pragma mark - setupUI

- (void)setupUI {
    self.fakeNavView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FD_NavH);

    //状态栏 x:44  其他:20
    UIView *fakeNavBgView = [[UIView alloc] initWithFrame:CGRectMake(0, FD_StatusBarHeight, SCREEN_WIDTH, FD_NavH - FD_StatusBarHeight)];
    [self.fakeNavView addSubview:fakeNavBgView];
    
    //一个返回按钮 + 一个标题
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [button setImage:[UIImage imageNamed:@"返回箭头.png"] forState:(UIControlStateNormal)];
    [button setImage:[UIImage imageNamed:@"返回箭头.png"] forState:(UIControlStateSelected)];
    //添加点击事件
    [button addTarget:self action:@selector(clickBackAction) forControlEvents:(UIControlEventTouchUpInside)];
    [fakeNavBgView addSubview:button];
    
    UILabel *titleLb = [[UILabel alloc] init];
    
    if ([NSString rb_ChineseLanguage]) {
        titleLb.text = @"搜索";
    }else {
        titleLb.text = @"Search";
    }
    
    [fakeNavBgView addSubview:titleLb];
    titleLb.textAlignment = NSTextAlignmentCenter;
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(fakeNavBgView.mas_left);//.offset(15);
        make.size.mas_equalTo(CGSizeMake(40, 30));
        make.centerY.equalTo(fakeNavBgView.mas_centerY);
    }];
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120, 30));
        make.centerY.equalTo(fakeNavBgView.mas_centerY);
        make.centerX.equalTo(fakeNavBgView.mas_centerX);
    }];
    
    self.searchView.frame = CGRectMake(0, self.fakeNavView.rb_bottom, SCREEN_WIDTH, 40);
    [self.view addSubview:self.fakeNavView];
    [self.view addSubview:self.searchView];
    
    [self setupSearchView];
}

- (void)clickBackAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupSearchView {

    //google自动搜索数据源
    self.tableDataSource = [[GMSAutocompleteTableDataSource alloc] init];
    self.tableDataSource.delegate = self; //设置代理GMSAutocompleteTableDataSourceDelegate
    
    //限制搜索结果
    GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
    filter.country = @"IE"; //(只搜索爱尔兰国的)
    self.tableDataSource.autocompleteFilter = filter;
    
    
    //搜索结果控制器
    self.resultControler = [[UITableViewController alloc] init];
    self.resultControler.tableView.delegate = self.tableDataSource;
    self.resultControler.tableView.dataSource = self.tableDataSource;
}




#pragma mark - 二 getter
- (UITableViewController *)resultControler {
    if (!_resultControler) {
        
        //创建对象
        _resultControler = [[UITableViewController alloc] init];
        //设置代理对象
        _resultControler.tableView.delegate = self.tableDataSource;
        _resultControler.tableView.dataSource = self.tableDataSource;
    }
    return _resultControler;
}

- (UIView *)fakeNavView {
    if (!_fakeNavView) {
        _fakeNavView = [UIView new];
        _fakeNavView.backgroundColor = [UIColor whiteColor];
    }
    return _fakeNavView;
}


- (HomeSearchTextView *)searchView {
    if (!_searchView) {
        _searchView = [[HomeSearchTextView alloc] initWithFrame:CGRectZero];
        //设置代理
        _searchView.delegate = self; //
    }
    return _searchView;
}


#pragma mark - 三 HomeSearchTextViewDelegate

//点击了输入框
- (void)homesearchTextFieldDidChange:(UITextField *)tf {
    //输入框内容发生变回就会触发该方法
    //触发搜索回调方法
    [self.tableDataSource sourceTextHasChanged:tf.text];
}

//开始输入
- (void)homesearchTextViewDidBeginEditing:(UITextField *)textField {
    [self fdShowResultViewController];
}

//结束输入
- (void)homesearchTextViewDidEndEditing:(UITextField *)textField {
    self.searchView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    [self fdHideResultViewController];
}

#pragma mark - 三 GMSAutocompleteTableDataSourceDelegate

//!!!!:搜索
//@required点击搜索结果代理上面的cell的时候会被调用
//要干什么:回收键盘,给textField赋值,隐藏搜索结果控制器
- (void)tableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource
didAutocompleteWithPlace:(GMSPlace *)place {
    FDLog(@"xxxx:%@--%@",place.name,place.formattedAddress);
    
    [self.searchView homesearchViewResignFirstResponser]; //回收键盘
    [self.searchView homesearchViewUpdateTFStr:place.formattedAddress placeholder:@""];
    [self fdHideResultViewController]; //隐藏搜索结果控制器
    
    //回调
    if (self.clickMySearchVCBlock) {
        self.clickMySearchVCBlock(place.coordinate, place.formattedAddress);
        //返回上一界面
        [self.navigationController popViewControllerAnimated:YES];
    }
}


//@required
//要干什么:回收键盘
- (void)tableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource
didFailAutocompleteWithError:(NSError *)error {
    FDLog(@"yyyy:%@",[error localizedDescription]);
}
//调用了 [weakSelf.tableDataSource sourceTextHasChanged:searchStr];
//这行代码会被触发:请求搜索数据
//要干什么:reloadData，显示菊花
- (BOOL)tableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource
    didSelectPrediction:(GMSAutocompletePrediction *)prediction {
    RBFunctionLog
    return YES;
}

//请求到数据的回调
//加载数据显示菊花
- (void)didRequestAutocompletePredictionsForTableDataSource:
(GMSAutocompleteTableDataSource *)tableDataSource {
    RBFunctionLog
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self.resultControler.tableView reloadData];
}

//要干什么:回收键盘,隐藏菊花
- (void)didUpdateAutocompletePredictionsForTableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.resultControler.tableView reloadData];
}


//Type argument 'NSInteger' (aka 'long') is neither an Objective-C object nor a block type
/**显示搜索控制器*/
- (void)fdShowResultViewController {
    self.resultControler.view.alpha = 0.0f;
    
    //显示搜索结果控制器
    //添加子视图控制器
    [self addChildViewController:self.resultControler];
    
    //添加子视图控制器的view
    self.resultControler.view.frame = CGRectMake(0, self.searchView.rb_bottom, SCREEN_WIDTH, 300);
    [self.view addSubview:self.resultControler.view];
    
    //reload data
    [self.resultControler.tableView reloadData];
    
    //动画
    [UIView animateWithDuration:0.5 animations:^{
        self.resultControler.view.alpha = 1.0f;
    } completion:^(BOOL finished) {
        [self.resultControler didMoveToParentViewController:self]; //已经移动到父视图控制器。
    }];
}

/**隐藏搜索控制器*/
- (void)fdHideResultViewController {
    [self.resultControler willMoveToParentViewController:nil];
    
    [UIView animateWithDuration:0.45 animations:^{
        //透明度为0.0f
        self.resultControler.view.alpha = 0.0f;
    } completion:^(BOOL finished) {
        //移除子视图控制器de view
        [self.resultControler.view removeFromSuperview];
        //移除子视图控制器
        [self.resultControler removeFromParentViewController];
    }];
    
    // 1-23回收视图跳动,黑屏问题。
    self.view.backgroundColor = kMainLightGrayColor;
    self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*2);
    self.searchView.frame = CGRectMake(0, FD_NavH, SCREEN_WIDTH, 40);
}


#pragma mark - 导航条有问题,直接关了写个假的
//- (UIView *)fakeNavView {
//    if (!_fakeNavView) {
//        _fakeNavView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FD_NavH)];
//        //一个按钮
//        //一个标题
//    }
//    return _fakeNavView;
//}

@end
